package org.sipsl.gisystems.gentest.gui;


import org.zkoss.lang.Strings;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.metainfo.ComponentInfo;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Label;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

public class LoginViewCtlr extends SelectorComposer<Window> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 542070718347984757L;

	/**
	 * 
	 */
	@Wire
	private Textbox nameTxb, passwordTxb;
	
	@Wire
	private Label mesgLbl;
	
	@Listen("onClick=#confirmBtn")
	public void confirm() {
		doLogin();
	}
	private void doLogin(){
		System.out.println("doLogin " + Sessions.getCurrent().getAttribute("user"));
		if (Sessions.getCurrent().getAttribute("user")!= null){
			Executions.sendRedirect("rtePage.zul");
			return;
		}
		String user = nameTxb.getValue();
		if(Strings.isBlank(user)){
			mesgLbl.setValue("*Need Rte user name!");
			return;
		}
		if (UserDAO.userExist(user)){
			mesgLbl.setValue("welcome " + user);
			Sessions.getCurrent().setAttribute("user",user);
			Executions.sendRedirect("rtePage.zul");
		}else{
			Executions.sendRedirect("login.zul");
		}
		//Sessions.getCurrent().setAttribute("user",user);
//		loginDiv.setVisible(false);
//		userDiv.setVisible(true);
//		userName.setValue(user);
		//Executions.sendRedirect("index.zul");
	}
	
	@Override
	public void doAfterCompose(Window comp) throws Exception {
		super.doAfterCompose(comp);
	}
	@Override
	public ComponentInfo doBeforeCompose(Page page, Component parent, ComponentInfo compInfo){
		if (Sessions.getCurrent().getAttribute("user")!= null){
			Executions.sendRedirect("rtePage.zul");
		}
		return super.doBeforeCompose(page, parent, compInfo);
	}
}
